package cn.zz.veggie.persistence.repository;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.apache.ibatis.executor.BatchResult;
import org.springframework.stereotype.Repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.zz.comm.core.types.query.PageQuery;
import cn.zz.comm.exception.classes.FatalErrorException;
import cn.zz.comm.util.CollectorUtil;
import cn.zz.comm.util.NumberUtil;
import cn.zz.veggie.model.domain.OrderInfo;
import cn.zz.veggie.model.query.OrderInfoQuery;
import cn.zz.veggie.persistence.dao.OrderInfoMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 订单明细表(OrderItem)repo
 *
 * @author zhongq
 * @since 2024-05-25 16:18:54
 */
@Slf4j
@Repository
@RequiredArgsConstructor
public class OrderInfoRepository {

	private final OrderInfoMapper orderInfoMapper;

	public OrderInfo findById(Integer id) {
		if (NumberUtil.isPositive(id)) {
			return orderInfoMapper.selectById(id);
		}
		return null;
	}

	public Map<Integer, OrderInfo> findByIds(Collection<Integer> ids) {
		if (CollectionUtil.isEmpty(ids)) {
			return Collections.emptyMap();
		}
		final List<OrderInfo> infos = orderInfoMapper.selectBatchIds(ids);
		return CollectorUtil.btmByKeyFun(infos, OrderInfo::getId);
	}

	public List<OrderInfo> findByPage(OrderInfoQuery query, PageQuery page) {
		LambdaQueryWrapper<OrderInfo> queryWrapper = query.toQueryWrapper();
		final Page<OrderInfo> queryPage = query.toPage(page);
		return orderInfoMapper.selectList(queryPage, queryWrapper);
	}

	public void save(OrderInfo orderItem) {
		Objects.requireNonNull(orderItem);
		if (orderItem.getId() == null) {
			// insert
			int effect = orderInfoMapper.insert(orderItem);
			if (effect != 1) {
				throw new FatalErrorException("created orderItem fail ");
			}
			log.info("created orderItem for {}", orderItem.getId());
		} else {
			// update
			int effect = orderInfoMapper.updateById(orderItem);
			if (effect != 1) {
				throw new FatalErrorException("update orderItem fail ");
			}
			log.info("updated orderItem for {}", orderItem.getId());
		}
	}

	public void deleteById(Integer id) {
		Objects.requireNonNull(id);
		final int effect = orderInfoMapper.deleteById(id);
		if (effect != 1) {
			log.error("delete");
			throw new FatalErrorException("delete orderItem fail ");
		}
		log.info("delete orderItem for {}", id);
	}

	public void deleteByIds(Collection<Integer> ids) {
		if (CollUtil.isEmpty(ids)) {
			return;
		}
		final int effect = orderInfoMapper.deleteByIds(ids);
		log.info("delete orderItem for {}", effect);
	}

	public void saveBatch(List<OrderInfo> items) {
		if (CollUtil.isEmpty(items)) {
			log.warn("order item save is empty");
			return;
		}
		final List<BatchResult> insert = orderInfoMapper.insert(items);
		log.info("insert order item for {} effect {}", items.get(0).getOrderId(), insert.size());
	}

	public List<OrderInfo> findByOrderIds(Collection<Integer> orderIds) {
		if (CollUtil.isEmpty(orderIds)) {
			return Collections.emptyList();
		}
		final OrderInfoQuery query = OrderInfoQuery.ofOrderIds(orderIds);
		return findByPage(query, PageQuery.all());
	}

}